/*
 * MenuHandler.cc
 *
 *  Created on: Sep 5, 2011
 *      Author: walter
 */

#include "MenuHandler.h"
#include <stdlib.h>
#include <iostream>
using namespace std;

MenuHandler::MenuHandler(Admin * admin) {
	this->admin = admin;
	menufunctions = new vector<Menufunction *>();
}

void MenuHandler::showMenu() {
	char inBuf[100];
	menufunctions = admin->getMenufunctions();
	while(true) {
		system("clear");
		cout << "\n\n\n\tUserinterface " << admin->getDescription() << endl;
		cout << "\n\t\t\tChoose Menuitem\n\n";
		for (int i = 0; i < menufunctions->size(); i++) {
			cout << "\t\t" << i+1 << "\t" <<
				(*menufunctions)[i]->getfunctiondescription() << endl;
		}
		cout << "\t\t" << menufunctions->size()+1 << "\texit\n";
		cout << "\n\t\tChoose:";
		cin.getline(inBuf, 100);
		int chosenindex = inBuf[0] -'0';
		if(chosenindex >= 1 &&  chosenindex <= menufunctions->size()) {
			(admin->*((*menufunctions)[chosenindex - 1]->getfunction()))();
		}
		if(chosenindex == menufunctions->size()+1) {
			exit(0);
		}
}
}
